<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>员工管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" media="all">
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">搜索</label>
                            <div class="layui-input-inline">
                                <input type="text" name="keyword" placeholder="请输入员工姓名/手机" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">所属门店</label>
                            <div class="layui-input-inline">
                                <select name="store_id">
                                    <option value="">全部门店</option>
                                    {foreach $stores as $store}
                                    <option value="{$store.id}">{$store.name}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-staff-search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <div style="padding-bottom: 10px;">
                    <button class="layui-btn layuiadmin-btn-list" data-type="add">添加员工</button>
                </div>

                <table id="LAY-staff-list" lay-filter="LAY-staff-list"></table>

                <script type="text/html" id="table-staff-operation">
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                </script>
            </div>
        </div>
    </div>

    <script src="/app/admin/component/layui/layui.js"></script>
    <script src="/app/admin/component/pear/pear.js"></script>
    <script>
    layui.use(['table', 'form'], function(){
        var $ = layui.$
        ,table = layui.table
        ,form = layui.form;

        //员工列表
        table.render({
            elem: '#LAY-staff-list'
            ,url: '/app/ky_shouyin/staff/list'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width: 80, sort: true}
                ,{field: 'username', title: '登录账号', width: 120}
                ,{field: 'name', title: '员工姓名', width: 100}
                ,{field: 'phone', title: '手机号码', width: 120}
                ,{field: 'store_name', title: '所属门店', width: 120}
                ,{field: 'role', title: '角色', width: 100, templet: function(d){
                    var roles = {1: '管理员', 2: '店长', 3: '收银员'};
                    return roles[d.role] || '';
                }}
                ,{field: 'status', title: '状态', width: 100, templet: function(d){
                    return d.status == 1 ? '<span class="layui-badge layui-bg-green">启用</span>' : '<span class="layui-badge layui-bg-gray">禁用</span>';
                }}
                ,{field: 'created_at', title: '创建时间', width: 160}
                ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-staff-operation'}
            ]]
            ,page: true
            ,limit: 15
            ,height: 'full-220'
            ,text: {
                none: '暂无数据'
            }
        });

        //监听工具条
        table.on('tool(LAY-staff-list)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定删除该员工？', function(index){
                    $.ajax({
                        url: '/app/ky_shouyin/staff/delete'
                        ,type: 'post'
                        ,data: {id: data.id}
                        ,success: function(res){
                            if(res.code == 0){
                                layer.msg(res.msg, {icon: 1});
                                table.reload('LAY-staff-list');
                            }else{
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑员工'
                    ,content: '/app/ky_shouyin/staff/edit?id=' + data.id
                    ,area: ['800px', '500px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-staff-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field;
                            
                            $.ajax({
                                url: '/ky_shouyin/staff/edit'
                                ,type: 'post'
                                ,data: field
                                ,success: function(res){
                                    if(res.code == 0){
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-staff-list');
                                        layer.close(index);
                                    }else{
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }
                            });
                        });
                        
                        submit.trigger('click');
                    }
                });
            }
        });

        //监听搜索
        form.on('submit(LAY-staff-search)', function(data){
            var field = data.field;
            
            table.reload('LAY-staff-list', {
                where: field
            });
        });

        //事件
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加员工'
                    ,content: '/app/ky_shouyin/staff/add'
                    ,area: ['800px', '500px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-staff-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field;
                            
                            $.ajax({
                                url: '/ky_shouyin/staff/add'
                                ,type: 'post'
                                ,data: field
                                ,success: function(res){
                                    if(res.code == 0){
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('LAY-staff-list');
                                        layer.close(index);
                                    }else{
                                        layer.msg(res.msg, {icon: 2});
                                    }
                                }
                            });
                        });
                        
                        submit.trigger('click');
                    }
                }); 
            }
        };

        $('.layui-btn.layuiadmin-btn-list').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
    </script>
</body>
</html> 